package no.ntnu.fp.gui;

import java.awt.Dimension;
import java.util.Calendar;
import java.util.Locale;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class YearChooser extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private JSpinner jspYear;
	private Locale locale;
	protected int year;
	private SpinnerModel yearModel;
	private Calendar calendar;
	protected DayChooser daychooser;

	public YearChooser() {
		setName("JYearChooser");
	    locale =Locale.getDefault();
	    calendar = Calendar.getInstance(locale);
	    daychooser = null;
	    int currentYear = calendar.get(Calendar.YEAR);
	    yearModel = new SpinnerNumberModel(currentYear, currentYear-100, currentYear+100, 1);
	    jspYear = new JSpinner(yearModel);
	    jspYear.setEditor(new JSpinner.NumberEditor(jspYear, "####"));
	    jspYear.setPreferredSize(new Dimension(100, 30));
	    jspYear.addChangeListener(new YearChangeListener());
	    setYear(Calendar.getInstance().get(Calendar.YEAR));
	    add(jspYear);
	}
	

	
	

	class YearChangeListener implements ChangeListener {
		public void stateChanged(ChangeEvent arg0) {
			if(arg0.getSource() == jspYear) {
				setYear((Integer)jspYear.getValue());
//				daychooser.setYear((Integer)jspYear.getValue());
			}
		}
		
	}
	
	public void setYear(int y) {
		int oldYear = year;
		year = y;
		if (daychooser != null)
		//	System.out.println("I am year chooser");
			daychooser.setYear(year);
		firePropertyChange("year", oldYear, year);
	}
	
	public int getYear() {
		return year;
	}
	
	public DayChooser getDaychooser() {
		return daychooser;
	}


    public void setDaychooser(DayChooser daychooser) {
		this.daychooser = daychooser;
	}

	
	public static void main(String args[]) {
		JFrame jframe = new JFrame();
		jframe.add(new YearChooser());
		jframe.pack();
		jframe.setVisible(true);
	}

}
